import TableLyout from "@/components/commonPublic/tableLyout.vue";
import elBreadcrumb from "@/components/public/el-breadcrumb.vue"; //面包屑
import publicForm from "@/components/materialDataManage/fromCom.vue"; //模糊查询
import tree from "@/components/testDetectionManage/tree";
import TableData from "@/components/public/el-table"; //主页面列表
import CustomDialog from "@/components/sampleManage/fieldSampling/index.vue";
import formJs from "./regexDetail.js"; //新增弹框配置项

import {
  getUserCompanyTreeList, //左侧机构树接口
  getSampleNeedWorkList, //主页面列表接口
  getfSampletype, //获取样品种类
} from "@/api/request.js";
// import { statusType2 } from "@/utils/config"; //配置项
export default {
  name: "samplesLedger",
  components: {
    TableLyout,
    elBreadcrumb, //面包屑
    publicForm, //模糊查询
    tree, //左侧组织机构数
    TableData, //主页面列表
    CustomDialog, //弹框组件
  },
  data() {
    return {
      name: "samplesLedger", //按钮配置页面代号
      queryCriteriaData: {}, //分页
      searchObj: formJs.searchObj, //搜索条件

      // 列表
      btnObj: [], //按钮
      table_config: formJs.table_config,
      tableData: [], //列表数据列表
      // checked: true, //查看下级按钮
      loading: false, //列表数据加载loading
      getList: {
        fCompanyid: "", // 左侧选中组织机构id
        startDates: "", // 开始时间
        endDates: "", // 截至时间
        keyWord: "", // 上方查询条件样品名称
        page: 1, // 当前页码
        pageSize: 20, // 每页显示条数
        userId: "",
        keyType: "", // 样品类型
        modelType: "0",
        timeDates: "",
      }, // 列表给后端传参
      // 列表

      // 弹框组件
      currentRow: null, //列表详情
      isShow: false, //是否显示弹框
      statusNum: null, //点击那个按钮
      displaySwitching: 2, //2取样&收样；0取样；1收样
      dialogTitle: null,
      // 弹框组件

      // 左侧机构树
      treelength: false, //左侧树显示隐藏
      showName: "未选择公司",
      treeData: [], //左侧树状图数据
      defaultProps: {
        children: "children",
        label: "fShortname",
      },
      checkData: [],
      treeText: {
        companyId: null,
        text: null,
        userId: null,
      },
      // 左侧机构树

      // 下拉框树
      selectTreeData: [],
      selectCheckData: [],
      selectDefaultProps: {
        children: "children",
        label: "fSampletypename",
      },
      // 下拉框树
      userInfo: {}, //登录用户
    };
  },

  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem("GxBack-userInfo"));
    if (JSON.parse(sessionStorage.getItem("GxBack-testDetection_treeCheck"))) {
      var id = JSON.parse(
        sessionStorage.getItem("GxBack-testDetection_treeCheck")
      );
      this.checkData = [id.fCompanyid];
    }
    this.getfSampletypeFun();
    this.getDate();
    this.getTree();
    this.btnRole(this.name);
    this.table_config.operation.cols = [{
      prop: "fTeststatus",
      type: "primary",
      handleRow: this.handleRow,
    },];

    this.table_config.operation["specialClass"] = function (rowData) {
      var color = null;
      if (rowData == "待检") {
        color = "red !important";
      } else if (rowData == "在检") {
        color = "orange !important";
      } else if (rowData == "已检") {
        color = "green !important";
      } else {
        color = "#409eff !important";
      }
      return color;
    };
    // this.table_config.operation.cols = [
    //   {
    //     label: "查看",
    //     type: "fTeststatus",
    //     handleRow: this.handleRow,
    //   },
    // ];
  },
  methods: {
    btnRole(name) {
      if (this.userInfo.userId != "System") {
        var btnList = [];
        var ModuleList = JSON.parse(
          sessionStorage.getItem("GxBack-ModuleList")
        );
        if (ModuleList.length > 0) {
          ModuleList.map((item) => {
            if (item.fEncode == name) {
              btnList = item.modulebuttonList;
            }
          });
          btnList.map((item) => {
            item["disabled"] = false;
          });
          this.btnObj = btnList;
        }
      } else {
        this.btnObj = formJs.btnObj;
      }
    },
    getBtnName(name, status) {
      this.statusNum = status
      this.yj_edit(status)
    },
    yj_edit(num) {
      this.statusNum = num
      // this.dialogTitle = "闭合处理"
      if (num == 1) { // 导出
        if (this.dataFid != null) {
          // this.editText();
        } else {
          this.$message.warning({
            content: "请先选择需要导出的数据",
            duration: 1000
          });
        }
      }

    },
    // 左侧树结构
    getTree() {
      this.treeText = {
        companyId: this.userInfo.companyId,
        userId: this.userInfo.userId,
        // text: "4", // 0其他  1 砼拌合站 2水稳 3沥青
      };
      getUserCompanyTreeList(this.treeText)
        .then((res) => {
          if (res.errcode == 0) {
            var companyTree = res.data.userCompanyTreeList;
            this.treeData = this.commonJs.tranListToTreeData(companyTree);
            if (this.treeData[0].children == undefined) {
              this.treelength = false; //左侧组织机构是否显示
            } else {
              this.treelength = true;
            }

            if (this.checkData[0]) {
              this.showName = JSON.parse(
                sessionStorage.getItem("GxBack-testDetection_treeCheck")
              ).fShortname;

              this.getList.fCompanyid = this.checkData[0];
            } else {
              this.checkData[0] = this.treeData[0].fCompanyid;
              this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
              this.getList.fCompanyid = this.treeData[0].fCompanyid; //当前点击组织机构ID
            }
            this.yj_searchForm(this.getList); // 调用主页面列表接口
            // 列表
          } else {
            this.$message.error({
              content: res.msg,
              duration: 1000
            });

            return false;
          }
        })
        .catch(() => {
          return false;
        });
    },
    // 点击左侧机构树
    handleNodeClick(data) {
      this.showName = data.fShortname; //顶部显示机构名
      this.getList.fCompanyid = data.fCompanyid;
      this.getList.page = 1;
      this.getList.pageSize = 20;
      this.yj_searchForm(this.getList);
    },

    // 获取列表数据
    yj_searchForm(data) {
      // window.sessionStorage.setItem(
      //   "GxBack-rawMaterialManage_getList",
      //   JSON.stringify(data)
      // );
      var obj = {
        fCompanyid: data.fCompanyid, //左侧选中组织机构id
        startDates: data.startDates, //进场开始时间
        endDates: data.endDates, //进场结束时间
        keyWord: data.keyWord, // 上方查询条件样品名称
        page: data.page, //当前页码
        pageSize: data.pageSize, //每页显示条数
        userId: this.userInfo.userId, //查询待办或已办时传 列表页查询不传
        keyType: data.keyType, //上方查询条件材料种类
        modelType: "0",
      };
      this.loading = true;
      var that = this;
      getSampleNeedWorkList(obj)
        .then((res) => {
          console.log("列表", res);
          if (res.errcode == 0) {
            that.loading = false;
            that.tableData = res.data.pageInfo.list;
            this.queryCriteriaData = res.data.pageInfo;
          } else {
            this.$message.error({
              content: "列表数据获取失败" + res.msg,
              duration: 1000
            });
            this.loading = false;
          }
        })
        .catch((error) => {
          console.log("列表数据 接口出错" + error.msg);
          this.loading = false;
        });
    },

    // 查看
    yj_viewForm(row, label) {
      if (label == "样品编号") {
        this.statusNum = 7;
        this.dialogTitle = "查看样品信息";
        this.currentRow = row;
        this.isShow = true;
      }
    },
    // 点击当前行
    rowChange(val) {
      this.currentRow = val;
      return val;
    },

    //获取时间
    getDate() {
      if (JSON.parse(sessionStorage.getItem("GxBack-testDetection_getList"))) {
        var dataList = JSON.parse(
          sessionStorage.getItem("GxBack-testDetection_getList")
        );
        this.getList = {
          fCompanyid: dataList.fCompanyid, // 左侧选中组织机构id
          startDates: dataList.startDates, // 开始时间
          endDates: dataList.endDates, // 截至时间
          keyWord: "", // 上方查询条件样品名称
          page: 1, // 当前页码
          pageSize: 20, // 每页显示条数
          userId: this.userInfo.userId,
          keyType: dataList.keyType, // 样品类型
          modelType: "0",
          timeDates: [dataList.startDates, dataList.endDates],
        };
        this.value2 = [this.getList.startDates, this.getList.endDates];
      } else {
        this.getList.startDates = this.common.getDay(-90); // 开始时间
        this.getList.endDates = this.common.getDay(0); // 截至时间
        this.getList.timeDates = [
          this.getList.startDates,
          this.getList.endDates,
        ];
      }
    },

    // 弹框组件
    refreshList() {
      //刷新列表
      this.$nextTick(() => {
        this.yj_searchForm(this.getList);
      });
    },
    closeDialog() {
      this.isShow = false;
    },
    // 弹框组件




    // 筛选条件
    // 下拉框树
    getfSampletypeFun() {
      var data = {
        fChecktypenum: "0", //0自检1外委
        fMaterialtypeid: "", //材料进场材料种类id
      };
      getfSampletype(data)
        .then((res) => {
          if (res.errcode == 0) {
            // this.selectTreeData = this.tranListToTreeData(
            //   res.data.SampletypeList
            // );
            var arr = [{
              children: [],
              fParentId: "0",
              fSamplemodel: "",
              fSampletype: "0",
              fSampletypename: "==请选择=="
            }];
            this.selectTreeData = arr.concat(this.tranListToTreeData(
              res.data.SampletypeList
            ));
            console.log("arr", arr);
            console.log("this.selectTreeData", this.selectTreeData);
            this.searchObj[1].tree.treeData = this.selectTreeData;
            this.searchObj[1].tree.checkData = [
              this.selectTreeData[0].fSampletype,
            ];
          } else {
            this.$message.error({
              content: "获取样品种类" + res.msg,
              duration: 1000
            });
            this.loadingEditDialog_cl = false;
          }
        })
        .catch((error) => {
          console.log("获取样品种类 接口出错", error.msg);
          this.loadingEditDialog_cl = false;
        });
    },
    tranListToTreeData(data) {
      let result = [];
      if (!Array.isArray(data)) {
        return result;
      }
      // 清除已存在的数据  重新赋值
      data.forEach((item) => {
        delete item.children;
      });

      let map = {};
      data.forEach((item) => {
        map[item.fSampletype] = item;
      });

      data.forEach((item) => {
        let parent = map[item.fParentId];
        if (parent) {
          (parent.children || (parent.children = [])).push(item);
        } else {
          result.push(item);
        }
      });
      return result;
    },
    selectHandleNodeClick(data) {
      if (data.fSampletypename == "==请选择==") {
        this.getList.keyType = ""; //样品种类汉字
      } else {
        this.getList.keyType = data.fSampletypename; //样品种类汉字
      }
      this.yj_searchForm(this.getList);
    },
    // 下拉框树
    // 时间间隔
    selectTime(e) {
      if (e[0] != "" && e[1] != "") {
        this.getList.startDates = e[0] + " " + "00:00:00"; // 开始时间
        this.getList.endDates = e[1] + " " + "23:59:59"; // 截至时间
      } else {
        this.getList.startDates = ""; // 开始时间
        this.getList.endDates = ""; // 截至时间
      }
      this.getList.timeDates = [e[0], e[1]];
      this.yj_searchForm(this.getList);
    },
    // 筛选条件
  },
};